﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using Serviio.API;

namespace Serviio.WHS2011
{
    public partial class SourceDetailsForm : Form
    {
        private static readonly LogFacade log = LogFacade.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        public SourceDetailsForm()
        {
            InitializeComponent();
        }

        private void Initialize(ServiioConnection connection)
        {
            comboBoxSourceType.DataSource = new BindingSource(connection.Reference.OnlineRepositoryTypes, null);
            comboBoxSourceType.DisplayMember = "Value";
            comboBoxSourceType.ValueMember = "Key";

            if (!connection.CheckMinVersion("0.6.1"))
            {
                textBoxSourceName.Enabled = false;
                textBoxSourceName.BackColor = SystemColors.Control;
                checkBoxEnabled.Visible = false;
            }
        }

        public ServiioOnlineRepository New(ServiioConnection connection)
        {
            try
            {
                Text = "Create New Online Source";
                Initialize(connection);

                if (this.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    ServiioOnlineRepository.FileTypes fileType = ServiioOnlineRepository.FileTypes.Audio;
                    if (radioButtonPhotos.Checked)
                        fileType = ServiioOnlineRepository.FileTypes.Image;
                    if (radioButtonVideo.Checked)
                        fileType = ServiioOnlineRepository.FileTypes.Video;

                    return new ServiioOnlineRepository(
                        textBoxSourceName.Text,
                        (string)comboBoxSourceType.SelectedValue,
                        textBoxSourceURL.Text,
                        fileType,
                        null,
                        checkBoxEnabled.Checked);
                }
                return null;
            }
            catch (Exception ex)
            {
                log.Error(ex);
                return null;
            }
        }

        public bool Edit(ServiioConnection connection, ServiioOnlineRepository repository)
        {
            try
            {
                Text = "Edit Online Source";
                Initialize(connection);

                comboBoxSourceType.SelectedValue = repository.RepositoryType;
                textBoxSourceName.Text = repository.RepositoryName;
                textBoxSourceURL.Text = repository.ContentURL;
                checkBoxEnabled.Checked = repository.Enabled;
                radioButtonVideo.Checked = repository.FileType == ServiioOnlineRepository.FileTypes.Video;
                radioButtonMusic.Checked = repository.FileType == ServiioOnlineRepository.FileTypes.Audio;
                radioButtonPhotos.Checked = repository.FileType == ServiioOnlineRepository.FileTypes.Image;
                
                if (this.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    repository.RepositoryName = textBoxSourceName.Text;
                    repository.RepositoryType = comboBoxSourceType.SelectedValue.ToString();
                    repository.ContentURL = textBoxSourceURL.Text;
                    repository.Enabled = checkBoxEnabled.Checked;
                    repository.FileType = ServiioOnlineRepository.FileTypes.Audio;
                    if (radioButtonPhotos.Checked)
                        repository.FileType = ServiioOnlineRepository.FileTypes.Image;
                    if (radioButtonVideo.Checked)
                        repository.FileType = ServiioOnlineRepository.FileTypes.Video;
                    return true;
                }
                return false;
            }
            catch (Exception ex)
            {
                log.Error(ex);
                return false;
            }
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            try
            {
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            catch (Exception ex)
            {
                log.Error(ex);
            }
        }

    }
}
